﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace customer.dao.V2
{
    public class OracleSchemaReader : SchemaReaderBase
    {

        public OracleSchemaReader(string connectionString)
        {
            this.ConnectionString = connectionString;
        }

        public override SchemaResult GetSchema()
        {
            SchemaResult result = new SchemaResult();
            try
            {
                using (OracleConnection conn = new OracleConnection(this.ConnectionString))
                {
                    conn.Open();

                    DataTable dtResult = conn.GetSchema();
                    DataView dv = dtResult.DefaultView;
                    dv.RowFilter = "OWNER not in ('SYSMAN','SYSTEM','XDB','WMSYS','SYS','SH')";
                    dtResult = dv.ToTable();

                    List<Dictionary<string, object>> items = DATA_Util.DataTable2Dictionary(dtResult);

                    result.Data = items;
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.ToString();
            }

            return result;
        }

        public override SchemaResult GetSchema(string collectionName)
        {   
            SchemaResult result = new SchemaResult();
            try
            {
                using (OracleConnection conn = new OracleConnection(this.ConnectionString))
                {
                    conn.Open();

                    DataTable dtResult = conn.GetSchema(collectionName);
                    DataView dv = dtResult.DefaultView;
                    dv.RowFilter = "TYPE <> 'System' and  OWNER not in ('SYSMAN','SYSTEM','XDB','WMSYS','SYS','SH','TSMSYS','OLAPSYS','IX')";
                    dtResult = dv.ToTable();

                    List<Dictionary<string, object>> items = DATA_Util.DataTable2Dictionary(dtResult);

                    result.Data = items;
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.ToString();
            }

            return result;
        }

        public override SchemaResult GetSchema(string collectionName, string[] restrictionsArray)
        {
            SchemaResult result = new SchemaResult();
            try
            {
                using (OracleConnection conn = new OracleConnection(this.ConnectionString))
                {
                    conn.Open();

                    DataTable dtResult = conn.GetSchema(collectionName, restrictionsArray);


                    List<Dictionary<string, object>> items = DATA_Util.DataTable2Dictionary(dtResult);

                    result.Data = items;
                }

                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.ToString();
            }

            return result;
        }
        

    }
}
